package cn.iocoder.yudao.module.jikong.controller.app.jkProduct;

import cn.iocoder.yudao.framework.common.OpenStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.jikong.controller.admin.area.vo.JkAreaPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.area.vo.JkAreaRespVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkProduct.vo.JkProductPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.jkProductType.vo.JkProductTypePageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.project.vo.JkProjectPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.project.vo.JkProjectRespVO;
import cn.iocoder.yudao.module.jikong.controller.admin.resource.vo.JkResourcePageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.resource.vo.JkResourceRespVO;
import cn.iocoder.yudao.module.jikong.controller.admin.size.vo.JkSizePageReqVO;
import cn.iocoder.yudao.module.jikong.controller.admin.size.vo.JkSizeRespVO;
import cn.iocoder.yudao.module.jikong.controller.app.jkProduct.vo.JkAppProductPageReqVO;
import cn.iocoder.yudao.module.jikong.controller.app.jkProduct.vo.JkProductRespVO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.area.JkAreaDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProduct.JkProductDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProductType.JkProductTypeDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.project.JkProjectDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.resource.JkResourceDO;
import cn.iocoder.yudao.module.jikong.dal.dataobject.size.JkSizeDO;
import cn.iocoder.yudao.module.jikong.service.area.JkAreaService;
import cn.iocoder.yudao.module.jikong.service.jkProduct.JkProductService;
import cn.iocoder.yudao.module.jikong.service.jkProductType.JkProductTypeService;
import cn.iocoder.yudao.module.jikong.service.project.JkProjectService;
import cn.iocoder.yudao.module.jikong.service.resource.JkResourceService;
import cn.iocoder.yudao.module.jikong.service.size.JkSizeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "WEB疾控产品表")
@RestController
@RequestMapping("/jikong/jk-product")
@Validated
public class JkWebProductController {

    @Resource
    private JkProductService jkProductService;
    @Resource
    private JkProductTypeService jkProductTypeService;
    @Resource
    private JkProjectService jkProjectService;
    @Resource
    private JkSizeService jkSizeService;
    @Resource
    private JkAreaService jkAreaService;
    @Resource
    private JkResourceService jkResourceService;


    @GetMapping("/productTypeList")
    @PermitAll
    @Operation(summary = "获得产品分类精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<JkProductTypeDO>> getProductSimpleList() {
        JkProductTypePageReqVO pageReqVO = new JkProductTypePageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setStatus(OpenStatusEnum.ENABLE.getStatus());
        List<JkProductTypeDO> list = jkProductTypeService.getJkProductTypePage(pageReqVO).getList();
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获得疾控产品表分页")
    @PermitAll
    public CommonResult<PageResult<JkProductRespVO>> getJkProductPage(@Valid JkAppProductPageReqVO pageReqVO) {
        JkProductPageReqVO productPageReqVO = new JkProductPageReqVO();
        BeanUtils.copyProperties(pageReqVO, productPageReqVO);
        PageResult<JkProductDO> pageResult = jkProductService.getJkProductPage(productPageReqVO);
        PageResult<JkProductRespVO> bean = BeanUtils.toBean(pageResult, JkProductRespVO.class);
        //设置typeName
        bean.getList().forEach(item -> item.setTypeName(jkProductTypeService.getJkProductType(item.getTypeId()).getName()));
        return success(bean);
    }

    @GetMapping("/get")
    @Operation(summary = "获得疾控产品详情")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JkProductRespVO> getJkProduct(@RequestParam("id") Long id) {
        JkProductDO jkProduct = jkProductService.getJkProduct(id);
        JkProductRespVO bean = BeanUtils.toBean(jkProduct, JkProductRespVO.class);
        bean.setTypeName(jkProductTypeService.getJkProductType(bean.getTypeId()).getName());

        //产品点击次数+1
        jkProductService.updateJkProductCount(jkProduct.getId(), jkProduct.getClickCount());

        return success(bean);
    }

    /**
     * 热门样本产品，查询10个，根据点击数倒排
     */
    @GetMapping("/hotList")
    @Operation(summary = "获得热门样本产品")
    @PermitAll
    public CommonResult<List<JkProductRespVO>> getJkProductPage() {
        JkProductPageReqVO productPageReqVO = new JkProductPageReqVO();
        productPageReqVO.setPageSize(10);
        productPageReqVO.setPageNo(1);
        List<JkProductDO> pageResult = jkProductService.getJkProductHot();
        List<JkProductRespVO> bean = BeanUtils.toBean(pageResult, JkProductRespVO.class);
        //设置typeName
        bean.forEach(item -> item.setTypeName(jkProductTypeService.getJkProductType(item.getTypeId()).getName()));
        return success(bean);
    }

    @GetMapping("/recommendList")
    @Operation(summary = "获得推荐样本产品")
    @PermitAll
    public CommonResult<List<JkProductRespVO>> getRecommendList() {
        JkProductPageReqVO productPageReqVO = new JkProductPageReqVO();
        productPageReqVO.setPageSize(10);
        productPageReqVO.setPageNo(1);
        List<JkProductDO> pageResult = jkProductService.getJkProductRecommend();
        List<JkProductRespVO> bean = BeanUtils.toBean(pageResult, JkProductRespVO.class);
        //设置typeName
        bean.forEach(item -> item.setTypeName(jkProductTypeService.getJkProductType(item.getTypeId()).getName()));
        return success(bean);
    }

    @GetMapping("/static")
    @Operation(summary = "获得推荐样本产品")
    @PermitAll
    public CommonResult<Map<String, String>> getStatic() {
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("renti", "1.2万");
        resultMap.put("huxidao", "100");
        resultMap.put("bingdu", "5000");
        resultMap.put("huanjingyushipin", "2.5万");
        resultMap.put("chagndao", "800");
        return success(resultMap);
    }

    @GetMapping("/area-simple-list")
    @Operation(summary = "获得区域列表下拉框")
    @PermitAll
    public CommonResult<PageResult<JkAreaRespVO>> getJkAreaSimpleList(@Valid JkAreaPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkAreaDO> pageResult = jkAreaService.getJkAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkAreaRespVO.class));
    }



    @GetMapping("/project-simple-list")
    @Operation(summary = "根据区域id获得项目列表下拉框")
    @PermitAll
    public CommonResult<PageResult<JkProjectRespVO>> getJkProjectSimpleList(@Valid JkProjectPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkProjectDO> pageResult = jkProjectService.getJkProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkProjectRespVO.class));
    }

    @GetMapping("/size-simple-list")
    @Operation(summary = "获得规格list")
    @PermitAll
    public CommonResult<PageResult<JkSizeRespVO>> getJkSizeList(@Valid JkSizePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkSizeDO> pageResult = jkSizeService.getJkSizePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkSizeRespVO.class));
    }

    @GetMapping("/resource-simple-list")
    @Operation(summary = "获得样本来源下拉框")
    @PermitAll
    public CommonResult<PageResult<JkResourceRespVO>> getJkResourceList(@Valid JkResourcePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<JkResourceDO> pageResult = jkResourceService.getJkResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JkResourceRespVO.class));
    }


}
